<template>
  <el-dialog v-model="popvisible" title="发布公告" @closed="handleclose">
    <el-form-item label="影响区域" :label-width="formLabelWidth">
      <el-table :data="infArea" stripe border height="130px">
        <el-table-column property="site" label="区域名称" />
        <el-table-column property="pho" label="物业电话" />
      </el-table>
    </el-form-item>
    <el-form :model="notice">
      <el-form-item label="公告信息" :label-width="formLabelWidth">
        <el-input
          v-model="notice.content"
          :rows="4"
          type="textarea"
          placeholder="请输入公告信息"
        />
      </el-form-item>
      <el-form-item label="发布时间" :label-width="formLabelWidth">
        <el-date-picker
          v-model="notice.time"
          type="datetime"
          placeholder="请输入发布时间"
          size="small"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">重置</el-button>
        <el-button type="primary" @click="handleConfirm">发布</el-button>
      </span>
    </template>
  </el-dialog>
  <el-button v-if="subggbtn" class="subgg" @click="popvisible = !popvisible"
    >发布公告</el-button
  >
</template>

<script setup>
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref, watch } from 'vue'
import { useUsersStore } from '@/stores/user'
import { storeToRefs } from 'pinia'
import { useGlobalMap } from '@/utils/globalMap.js'
import { createDraw } from '@/zondy_api/draw.js'
import { constructAnnouncementCallback } from '../Hooks/fbggHuidiao'
import { clip } from '../Hooks/caijian'
import { clipsuc } from '../Hooks/cajiansuc'
var map
var source
var layer
let draw
// const $router=useRouter()
const popvisible = ref(false)
const subggbtn = ref(false)
const formLabelWidth = '100px'
let layeradd = []
//使用pina
const store = useUsersStore()
const { fbggDialog, activeIndex, fbggdata,traffic_id } = storeToRefs(store)
const notice = reactive({
  content: '',
  time: '',
})

//表格假数据
let infArea = ref([])
// 获取公告内容数据
const hangdleInput = (evt) => {
  // console.log(evt);
  notice.content = evt
}
// 获取公告发布时间
const hangdleDateInput = (e) => {
  // console.log(e);
  notice.time = e
}

//获取map
onMounted(() => {
  map = useGlobalMap()
  //创建图层
  source = new ol.source.Vector({})
  layer = new ol.layer.Vector({
    source,
  })
  map.addLayer(layer)
  //创建画笔
  draw = createDraw({
    type: 'LineString',
    source,
  })
})
watch(
  () => store.fbggDialog,
  () => {
    if (store.fbggDialog) {
      // console.log('fagg')
      map.addInteraction(draw)
      draw.on('drawend', function (evt) {
        //  const a= constructAnnouncementCallback(evt, map)
        //  console.log(result);
        subggbtn.value=true
        constructAnnouncementCallback({
          result: evt,
          bufferSuccess: (result) => {
            // console.log('缓冲分析成功')
            // console.log(result)
            clip({
              result,
              clipSuccess: (res) => {
                // console.log('裁剪分析成功')
                // console.log(res)
                layeradd.push(clipsuc(res, map))
                console.log(layeradd);
              },
            })
          },
        })
      })
    }
  }
)
watch(
  () => store.fbggdata,
  () => {
    infArea.value = []
    const arr1 = []
    store.fbggdata.map((item) => {
      arr1.push(item.AttValue)
    })
    arr1.map((item) => {
      // console.log(item)
      infArea.value.push({
        site: item[4],
        pho: item[5],
      })
    })
    // console.log(infArea.value)
    
  }
)
//关闭按钮执行的方法
function handleclose() {
  map.removeInteraction(draw)
  subggbtn.value = false
  store.fbggDialog = false
  store.fbggdata = []
  store.activeIndex = '0'
  console.log(layeradd)
  source.clear()
  layeradd.map((item) => {
    map.removeLayer(item)
  })
}
const handleConfirm = () => {
  axios
    .post(`http://localhost:3000/notice`, { traffic_id:store.traffic_id, notice })
    .then((res) => {
      // console.log(res);
      const { code } = res.data
      if (code === 0) {
        ElMessage({
          message: '发布公告成功',
          type: 'success',
        })
      }
    })
  notice.content = ''
  notice.time = ''
  popvisible.value=false
  handleclose()
}
//重置方法
const handleCancel = () => {
  notice.content = ''
  notice.time = ''
  fbggDialog.value=false
}
</script>
<style scoped>
.el-button--text {
  margin-right: 0px;
}
.el-input {
  width: 250px;
}
.dialog-footer button:first-child {
  margin-right: 0px;
}
.el-table__row {
  height: 20px;
}
.el-table__body tr.el-table__body td {
  padding: 0;
  height: 40px;
}
.subgg {
  position: fixed;
  top: 50px;
  right: 20px;
}
</style>
